// 2110101 : Lab6 (2545)
// dept. of computer engineering
// Chulalongkorn Univ.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class XBall1 extends Applet implements Runnable {
Thread ballThread;
double x, y, dx, dy;
int radius;
public void run() {
int frameWidth, frameHeight;
setBallProperties();
// add your local variable declarations here
while (ballThread == Thread.currentThread()) {
frameWidth = getSize().width;
frameHeight = getSize().height;
// add your code here
// (x, y) is the ball's position
// (dx, dy) is the velocity vector
// frameWidth and frameWidth are width and height of the bounding frame.
//
// The following code makes the ball bouncing inside the frame
// forever with no gravity and no loss of energy
if (x <= radius || x >= frameWidth - radius) dx = -dx;
if (y <= radius || y >= frameHeight - radius) dy = -dy;
else dy+= 1;
x += dx;
y += dy;
repaint(); // paint the screen
try { // wait for 30msec
Thread.sleep(30);
} catch (InterruptedException e) {}
}
} // end of run method
public void init() {
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
}
void this_mouseClicked(MouseEvent e) {
this.stop();
this.start();
}
public void start() {
ballThread = new Thread(this);
ballThread.start();
}
public void stop() {
ballThread = null;
}
private void setBallProperties() {
radius = 5 + (int) (15 * Math.random());
x = radius + (int) ((getSize().width - 2 * radius) * Math.random());
y = radius + 1 + (int) (50 * Math.random());
dx = 1 + (2 * Math.random());
dy = 1 + (2 * Math.random());
if (Math.random() < 0.5) dx = -dx;
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval((int) x - radius, (int) y - radius, 2 * radius, 2 * radius);
}
// main method
public static void main(String[] args) {
Frame frame = new Frame("XBall1");
XBall1 applet = new XBall1();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.add(applet, BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setVisible(true);
applet.init();
applet.start();
}
}
// 2110101 : Lab6 (2545)
// dept. of computer engineering
// Chulalongkorn Univ.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class XBall2 extends Applet implements Runnable {
Thread ballThread;
double x, y, dx, dy;
int radius;
public void run() {
int frameWidth, frameHeight;
setBallProperties();
// add your local variable declarations here
while (ballThread == Thread.currentThread()) {
frameWidth = getSize().width;
frameHeight = getSize().height;
// add your code here
// (x, y) is the ball's position
// (dx, dy) is the velocity vector
// frameWidth and frameWidth are width and height of the bounding frame.
//
// The following code makes the ball bouncing inside the frame
// forever with no gravity and no loss of energy
if (x <= radius || x >= frameWidth - radius) dx = -dx;
if (y <= radius || y >= frameHeight - radius) dy = -dy;
if (y < frameHeight - radius) dy = dy + 1;
x += dx;
y += dy;
double le = 0.95;
if (x <= radius || x >= frameWidth - radius) dx = -dx;
if (y <= radius || y >= frameHeight - radius) {
dy = -dy *.8;
dx = dx;
} else dy+= 9.8*0.3;
repaint(); // paint the screen
try { // wait for 30msec
Thread.sleep(30);
} catch (InterruptedException e) {}
}
} // end of run method
public void init() {
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
}
void this_mouseClicked(MouseEvent e) {
this.stop();
this.start();
}
public void start() {
ballThread = new Thread(this);
ballThread.start();
}
public void stop() {
ballThread = null;
}
private void setBallProperties() {
radius = 5 + (int) (15 * Math.random());
x = radius + (int) ((getSize().width - 2 * radius) * Math.random());
y = radius + 1 + (int) (50 * Math.random());
dx = 1 + (2 * Math.random());
dy = 1 + (2 * Math.random());
if (Math.random() < 0.5) dx = -dx;
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval((int) x - radius, (int) y - radius, 2 * radius, 2 * radius);
}
// main method
public static void main(String[] args) {
Frame frame = new Frame("XBall2");
XBall2 applet = new XBall2();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.add(applet, BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setVisible(true);
applet.init();
applet.start();
}
}
|